<?php
require_once("includes.php");
include('database.php');

$products = array();
$products = get_products();
$products_count = get_products_count();
$error = false;

if(isset($_GET['productid'])){
    /* Wenn ein Produkt gezeigt werden soll */
    $productid = $_GET['productid'];
    $result = mysql_fetch_array(mysql_query("SELECT MAX(ID) FROM products"));
    $productid_max = $result[0];
    if(($productid_max < $productid) || ($productid == 0)){
        $error = true;
        $title_suffix = " - Fehler";
        $product_desc = "Dieses Produkt existiert nicht.";
        $product_name = "That's an error. That's all we know.";
    }else{
        //Alles über das gewählte Produkt auslesen
        $result = mysql_fetch_array(mysql_query("SELECT * FROM products WHERE id = $productid"));
        //Produktnamen auslesen
        $product_name = $result[1];
        //Preis des Produktes auselsen
        $product_price = $result[2];
        //Produktbeschreibung auslesen
        $product_desc = $result[3];
        //Produktbeschreibung (kurz) auslesen
        $product_descs = $result[4];
        //Masse des Produktes
        $product_mass = $result[5];
        //Gewicht des Produktes
        $product_weight = $result[6];
        //Leistung des Produktes
        $product_power = $result[7];
        //Suffix des Titels zusammensetzen
        $title_suffix = " - $product_name";
        $smarty->assign('descs', $product_descs);
        $smarty->assign('price', $product_price);
        $smarty->assign('mass', $product_mass);
        $smarty->assign('weight', $product_weight);
        $smarty->assign('power', $product_power);
    }
    $smarty->assign('title', $product_name);
    $smarty->assign('desc', $product_desc);

    $style = "style_product_desc.css";
    $tpl_file = "product_desc.tpl";
}else{
    /* Wenn nur die Auflistung der Produkte gezeigt werden soll. */
    $tpl_file = "products.tpl";
    $style = "products.css";
    $title_suffix = " - Produkte";
}
$smarty->assign('error', $error);
$smarty->assign('title_suffix', $title_suffix);
$smarty->assign('tpl_file', $tpl_file);
$smarty->assign('site_style', $style);
$smarty->assign('intro', false);
$smarty->assign('products', array($products));
$smarty->display('site.tpl');
?>
